<?php
namespace app\controllers;

use app\models\UploadFile;
use Yii;
use yii\web\Response;
use yii\web\UploadedFile;

/**
 * 定义文件上传接口
 *
 * Trait UploadControllerTrait
 * @package app\controllers
 */
trait UploadControllerTrait
{
    /**
     * 文件上传AJAX接口
     * @var $type integer 文件类型UploadFile::TYPE_*，如果没有从GET获取到，则尝试从上传地址自动判断
     * @var $file_field string 指定文件字段名称，如果没有设置，首先获取名称为files的列表，如果列表为空，再尝试获取file字段
     * @return array(fid, url) 如果正常返回文件编号和url
     */
    public function actionUpload()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $type = Yii::$app->request->get('type');
        if (empty($type)) { // 尝试从route中判断类型
            $type_map = [
                'admin/article/upload'=>UploadFile::TYPE_ARTICLE,
                'admin/ad/upload'=>UploadFile::TYPE_AD,
                'admin/system/upload'=>UploadFile::TYPE_SYSTEM,
            ];
            if (!isset($type_map[$this->route])) {
                return [
                    'message'=>'参数错误。'
                ];
            }
            $type = $type_map[$this->route];
        }
        $file_field = Yii::$app->request->get('file_field', 'files');
        $file_list = UploadedFile::getInstancesByName($file_field);
        if (empty($file_list)) { // 尝试读取单个文件
            $_file = UploadedFile::getInstanceByName('file');
            if (!empty($_file)) {
                $file_list[] = $_file;
            }
        }
        if (!empty($file_list)) {
            try {
                if (!is_array($file_list)) {
                    $file_list[] = $file_list;
                }
                $result = [];
                foreach ($file_list as $file) {
                    $upload_file = UploadFile::saveUpload($type, $file);
                    $result[] = [
                        'fid'=>$upload_file->id,
                        'url'=>$upload_file->getUrl(),
                    ];
                }
                return [
                    'result'=>'success',
                    'files'=>$result
                ];
            } catch (\Exception $e) {
                return [
                    'message'=>$e->getMessage(),
                ];
            }
        }
        return [
            'message'=>'没有找到上传文件。'
        ];
    }
    
    /**
     * 文件删除AJAX接口
     * @var $id integer 指定文件编号
     * @return array
     */
    public function actionDeleteUpload()
    {
        if (Yii::$app->user->isGuest) {
            return [
                'message'=>'没有登录。'
            ];
        }
        $id = Yii::$app->request->get('id');
        /* @var $upload_file UploadFile */
        $upload_file = UploadFile::find()->where(['uid'=>Yii::$app->user->id, 'id'=>$id])->one();
        if (empty($upload_file)) {
            return [
                'message'=>'没有找到文件。'
            ];
        }
        $upload_file->status = UploadFile::STATUS_DEL;
        if ($upload_file->save()) {
            return [
                'result'=>'success'
            ];
        }
        return [
            'message'=>'删除文件时出现错误。',
            'errors'=>$upload_file->errors
        ];
    }
}
